package json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JacksonMapper {

    public static void main(String[] args) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "cardProcessor");
        Map<String, Object> mx = new HashMap<>();
        mx.put("a", 1);
        mx.put("b", 2);
        map.put("data", mx);


        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        System.out.println(s);





        TypeReference<HashMap<String,Object>> typeRef
                = new TypeReference<HashMap<String,Object>>() {};

        HashMap<String,Object> o = mapper.readValue(s, typeRef);



        System.out.println("Got " + o);


        List<Map<String, Object>> lst = new ArrayList<>();
        lst.add(mx);
        lst.add(mx);


        s = mapper.writeValueAsString(lst);
        System.out.println(s);


        TypeReference<List<HashMap<String,Object>>> typeRef2
                = new TypeReference<>() {};

        List<HashMap<String,Object>> dl = mapper.readValue(s, typeRef2);
        System.out.println(dl.size());


    }
}
